import {ajaxDelete, ajaxGet, ajaxPost, ajaxPut} from "@/api/axios";
import vue from '../main';

const HttpUrl = 'http://127.0.0.1:8000'

const SUCCESS = 'success'

const ERROR = '0005'

const actions = {
    // eslint-disable-next-line no-unused-vars
    _POST_DATA(nul, {dataUrl, ...params}) {
        // eslint-disable-next-line no-async-promise-executor
        return new Promise(async (resolve) => {
            const url = `${HttpUrl}${dataUrl}`
            const res = await ajaxPost(url, {
                ...params,
            }, true)
            if (res.data) {
                if (res.data.status === SUCCESS) {
                    // if (res.data.data === null) {
                    //   res.data.data = []
                    // }
                    resolve(res.data)
                    vue.$successMessage('操作成功')
                } else if (res.data.code === ERROR) {
                    //   noRoleAction(res.data.message)
                    resolve(false)
                }
            } else {
                resolve(false)
            }
        })
    },

    _PUT_DATA(nul, {dataUrl, ...params}) {
        // eslint-disable-next-line no-async-promise-executor
        return new Promise(async (resolve) => {
            const url = `${HttpUrl}${dataUrl}`
            const res = await ajaxPut(url, {
                ...params,
            }, true)
            if (res.data) {
                if (res.data.status === SUCCESS) {
                    // if (res.data.data === null) {
                    //   res.data.data = []
                    // }
                    resolve(res.data)
                    vue.$successMessage('操作成功')
                } else if (res.data.code === ERROR) {
                    //   noRoleAction(res.data.message)
                    resolve(false)
                }
            } else {
                resolve(false)
            }
        })
    },

    // eslint-disable-next-line no-unused-vars
    _GET_DATA({commit}, {dataUrl, ...params}) {
        // eslint-disable-next-line no-async-promise-executor
        return new Promise(async (resolve) => {
            const url = `${HttpUrl}${dataUrl}`
            const res = await ajaxGet(url, {
                ...params,
            })
            if (res.data) {
                if (res.data.status === SUCCESS) {
                    // if (res.data.data === null) {
                    //   res.data.data = []
                    // }
                    resolve(res.data)
                    // vue.$successMessage('操作成功')
                } else if (res.data.code === ERROR) {
                    //   noRoleAction(res.data.message)
                    resolve(false)
                }
            } else {
                resolve(false)
            }
        })
    },
    _DELETE_DATA(nul, {dataUrl, ...params}) {
        // eslint-disable-next-line no-async-promise-executor
        return new Promise(async (resolve) => {
            const url = `${HttpUrl}${dataUrl}`
            const res = await ajaxDelete(url, {
                ...params,
            })
            if (res.data) {
                if (res.data.status === SUCCESS) {
                    // if (res.data.data === null) {
                    //   res.data.data = []
                    // }
                    resolve(res.data)
                    vue.$successMessage('操作成功')
                } else if (res.data.code === ERROR) {
                    //   noRoleAction(res.data.message)
                    resolve(false)
                }
            } else {
                resolve(false)
            }
        })
    },
}

export default actions